<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\common\model\FriendlinkModel;
use think\facade\Db;
use think\facade\Session;

	/**
	 * Created by Ailcc.com
     * 2021-11-14 12:00:00 沛霖主页
	 */
class Friendlink extends Common
{
    public function getList()
    {
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $keyword = input('keyword');

        $map = [];
        if(!empty($keyword)){
            $map[] = ['title','like','%'.$keyword.'%'];
        }
        $list = FriendlinkModel::pageList($map);
        return json(['code'=>0,'msg'=>'success','data'=>$list]);
    }

    public function del(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $id = $this->request->param('ids');
        FriendlinkModel::destroy($id);
        return json(['code'=>0,'msg'=>'删除成功']);
    }

    public function add(){
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        
        $data = $this->request->param('data');
        if(!empty($data['id'])){
            FriendlinkModel::update($data);
        }else{
            unset($data['id']);
            $data['admin_id'] = session('admin_id');
            FriendlinkModel::create($data);
        }
        return json(['code'=>0,'msg'=>'操作成功']);
    }
    
	/**
	 * Created by Ailcc.com
     * 2021-11-14 12:00:00 沛霖主页
     * 管理友情链接
	 */
    public function editShu() {
        $admin_id = Session::get('admin_id');
        if(empty($admin_id)){
            return json(['code'=>1, 'msg'=>'请先登录','data'=>[]]);
        }
        

        $userList = Db::name('link')->order('create_time DESC')->select();
        if ($this->request->isPost()) {
            $id         = input('post.id');
            $value      = input('post.value');
            $action     = input('post.action');
            $status     = input('post.status');
            $category   = input('post.category');
            if($value == 'true'){
                $value = '1';
            }elseif($value == 'false'){
                $value = '0';
                
            }
            // 更新留言信息
            if($category == 'status'){
                Db::name('link')->where('id', $id)->update([$status => $value]);
            }else{
                Db::name('link')->update([$category => $value,'id'=>$id]);
            }
            // 更新信息
            
            
            return json(['code'=>0,'msg'=>'操作成功']);
        }
        $this->getSide();
        return $this->fetch();
    }

}
